# https://leetcode-cn.com/contest/weekly-contest-176/problems/product-of-the-last-k-numbers/

class ProductOfNumbers:

    def __init__(self):
        self.l = [1]

    def add(self, num: int) -> None:
        if num == 0:
            self.l = [1]
        else:
            self.l.append(num * self.l[-1])

    def getProduct(self, k: int) -> int:
        if k >= len(self.l):
            return 0
        return self.l[-1] / self.l[-k-1]


def test_pd():
    p = ProductOfNumbers()
    p.add(3)
    p.add(0)
    p.add(2)
    p.add(5)
    p.add(4)
    assert p.getProduct(2) == 20
    assert p.getProduct(3) == 40
    assert p.getProduct(4) == 0
    p.add(8)
    assert p.getProduct(2) == 32
